<?php

class clogin extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Login');
        $this->load->library(array('session','form_validation'));
        $this->load->helper(array('url','form'));
        $this->load->database('default');
        
    }
    
    public function index()
    {    
        $datos['cabecera']= "Inicio Sesion";
        $data['titulo']="AUDITAR";
        $this->load->view('includes/header.php',$data);
        $session_data =  $this->session->userdata('perfil');
        $session_data['rol'] =$this->session->userdata('perfil');
        switch ($this->session->userdata('perfil')) {
            case '':
                $data['token'] = $this->token();
                $data['titulo'] = 'Login con roles de usuario';
                $this->load->view('viewLogin',$data);
                break;
            case 'administrador':
                //redirect(base_url().'admin');
                redirect('welcome');
                break;
            case 'recepcion':
                //redirect(base_url().'editor');
                redirect('welcome');
                break;    
            case 'suscriptor':
                //redirect(base_url().'suscriptor');
                redirect('welcome');
                break;
            default:        
                $data['titulo'] = 'Login';
                $this->load->view('viewLogin',$data);
                break;        
        }
        $this->load->view('includes/footer.php');
    }
 
public function new_user()
    {
        if($this->input->post('token') && $this->input->post('token') == $this->session->userdata('token'))
        {
            $this->form_validation->set_rules('nombre', 'nombre de usuario', 'required|trim|min_length[2]|max_length[150]|xss_clean');
            $this->form_validation->set_rules('password', 'password', 'required|trim|min_length[5]|max_length[150]|xss_clean');
 
            //lanzamos mensajes de error si es que los hay
            
            if($this->form_validation->run() == FALSE)
            {
                $this->index();
            }else{
                $nombre = $this->input->post('nombre');
                $password = sha1($this->input->post('password'));
                $check_user = $this->Login->login_user($nombre, $password);
                if($check_user == 1)
                { 
                    $data = array(
                    'is_logued_in'=>TRUE,
                    'id'=> $check_user->id,
                    'id_rol'=> $check_user->id_rol,
                    'nombre'=> $check_user->nombre
                    );        
                    $this->session->set_userdata($data);
                    //$this->index();
                    redirect(base_url().'welcome');
                   
                }
            }
        }else{
            redirect('clogin');
        }
    }
    
     public function token()
    {
        $token = md5(uniqid(rand(),true));
        $this->session->set_userdata('token',$token);
        return $token;
    }
    
    public function logout_ci()
    {
        $this->session->sess_destroy();
        $this->index();
    }
    
    
    
    function cargarUsuariosPrueba()
    {
        $this->load->model('Usuario','',TRUE);
        $this->Usuario->nuevoParametro('ed','ed',1);
        $this->Usuario->nuevoParametro('laura','laura',1);        
        
        $this->load->view('welcome');
    }
    
    function logearUsuario(){
        $bandera = true;      
        $variable=$this->session->userdata('usuario');       
      
            if(!empty($variable))
            {
                redirect(base_url().'welcome');
                exit();
            }
        
       if($this->input->post() && !empty($_POST['usuario']))
       {
          
            
            $usuario = $_POST['usuario'];
            $contrasenia = $_POST['password'];
            $existe = $this->Login->validar_usuario($usuario,$contrasenia);
            
            $data_header['titulo']=$existe;
            if($existe['existe'])
            {      
                $this->session->set_userdata('usuario',$existe['usuario']->usuario);
                $this->session->set_userdata('rol',$existe['usuario']->t_rol_id);
                $this->session->set_userdata('id', $existe['usuario']->id);
                $this->session->set_userdata('is_logued_in',TRUE);
                $this->Login->token($this->session->userdata('id'));
                                
                
                $data_header['titulo']=  "Seguridad Alimentaria - Inicio Sesion " ; 
                
                
                
                $this->load->view('includes/header.php',$data_header);
                $this->load->view('welcome_message');
                $this->load->view('includes/footer.php');                
            }
            else
            {                
                $this->session->sess_destroy();
                 redirect(base_url().'clogin');
            }
           
       }
       
       

    }
    
    function salir(){
        
         $variable=$this->session->userdata('usuario');       
      
            if(!empty($variable))
            {
                $this->session->sess_destroy();
                redirect(base_url().'clogin');
                
            }
            else
            {
                redirect(base_url().'clogin');
            }
        
    }
}
?>